/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.appengine.gar.client.ui;

import nl.biz_e.appengine.gar.client.GastouderRegistratie;
import nl.biz_e.appengine.gar.client.i18n.Constants;
import nl.biz_e.appengine.gar.client.i18n.GarConstants;
import nl.biz_e.appengine.gar.client.services.LoginService;
import nl.biz_e.appengine.gar.client.services.LoginServiceAsync;
import nl.biz_e.appengine.gar.shared.User;
import nl.biz_e.gwt.baselib.ui.client.panels.StdLoginPanel;
import nl.biz_e.gwtappengine.lib.client.BaseAppEngineAppPanel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;

/**
 * Shows the {@link StdLoginPanel} for logging in.
 * 
 * @author Joost A. Bloemsma
 */
public class LoginPanel extends BaseAppEngineAppPanel {
    private final GarConstants constants = Constants.getGarConstants();

    /**
     * Constructs a new LoginPanel instance
     */
    public LoginPanel() {
        super();
        this.add(getLoginPanel());
    }

    void onSubmitted(User user) {
        GastouderRegistratie appMan = (GastouderRegistratie) this
                .getAppManager();
        if (user != null) {
            appMan.loggedIn(user);
        } else {
            appMan.showMessage(constants.loginfailure());
        }
    }

    private Panel getLoginPanel() {
        final FormPanel form = new FormPanel();

        final StdLoginPanel lp = new StdLoginPanel(
                "(Try admin/admin for test)", new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        form.submit();
                    }
                });
        form.add(lp);

        form.addSubmitHandler(new FormPanel.SubmitHandler() {
            @Override
            public void onSubmit(SubmitEvent event) {
                LoginServiceAsync loginSrvc = GWT.create(LoginService.class);
                getAppManager().showBusy();
                loginSrvc.login(lp.getName(), lp.getPassword(),
                        new AsyncCallback<User>() {
                            @Override
                            public void onSuccess(User result) {
                                onSubmitted(result);
                            }

                            @Override
                            public void onFailure(Throwable caught) {
                                LoginPanel.this.getAppManager().showMessage(
                                        caught.getMessage());

                            }
                        });
                event.cancel();
                getAppManager().hideBusy();
            }
        });
        return form;
    }

}
